Skip to content

Fix duplicate module loading: exclude direct script tags from import map#101

Closed
hihumanzone with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-duplicate-javascript-injection
Closed

Fix duplicate module loading: exclude direct script tags from import map#101
hihumanzone with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-duplicate-javascript-injection

Conversation

Copilot AI commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Modules referenced via <script type="module" src="main.js"> are loaded twice — once from the rewritten script tag (blob URL) and again from the generated import map. Functionally harmless but wasteful.

Fix: Track which modules are already loaded as direct <script> entries during replaceScriptTags, then exclude them from the import map.

  • replaceScriptTags — accepts optional directModuleScripts Set, records each resolved module path when rewriting a script tag
  • buildModuleImportMap / buildModuleImportMapScript — accepts optional excludePaths Set, skips matching entries
  • replaceAssetReferences, injectConsoleScript — thread the Set through the call chain
  • generateFullDocumentPreview, generateMultiFilePreview — create the Set and pass it through both asset replacement and import map generation
  • replaceDownloadLinks — creates a per-sub-page Set for linked HTML files

All new parameters are optional with no behavioral change when omitted.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel

vercel Bot commented Mar 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
html-previewer Ready Ready Preview, Comment Mar 1, 2026 11:13am

…gs from import map

When an HTML file contains <script type="module" src="main.js">, the src is
rewritten to a blob URL. Previously, the same file was also included in the
generated import map, causing it to load twice. Now, files that are directly
loaded via script tags are tracked and excluded from the import map.

Co-authored-by: hihumanzone <101509991+hihumanzone@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate JavaScript injection issue Fix duplicate module loading: exclude direct script tags from import map Mar 1, 2026
@hihumanzone

Copy link
Copy Markdown
Owner

@copilot

I have a conceptual question.

If a file is loaded directly via the rewritten <script type="module"> tag (and not through the generated import map), can it export functions in a way that other modules can import? Or would that not work?

If it cannot be imported by other modules, then keeping both the rewritten script tag and the generated import map might actually serve a purpose — is that correct?

Copilot AI commented Mar 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot

I have a conceptual question.
...

Great question — the answer is: no, the script-tag version and the import map version were never the same module instance, so removing the import map entry for the directly-loaded file does not break cross-module imports.

Here's why. Every call to URL.createObjectURL() returns a new, unique URL. So:

  • createModuleAssetUrl (used for the <script> tag) creates blob URL X
  • buildModuleImportMap (for the import map) calls createRewrittenModuleBlobUrl separately and creates blob URL Y

Browsers cache ES modules by URL. Because X ≠ Y, they are always two entirely separate module instances. Before the fix:

  • <script type="module" src="blob:X"> loads instance X
  • @preview/main.js → blob:Y loads instance Y
  • Any other module that did import ... from "@preview/main.js" would get instance Y — completely unrelated to X

So the import map entry for a directly-loaded script never enabled other modules to reach that same instance. The only thing it was doing was loading the file a second time. Removing it from the import map (this fix) is therefore safe: other modules that import @preview/main.js simply get whatever is in the import map — which remains unaffected for every file that is not a direct script tag entry point.

@hihumanzone hihumanzone closed this Mar 1, 2026
@hihumanzone hihumanzone deleted the copilot/fix-duplicate-javascript-injection branch March 1, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants